package dk.itu.spvc.tourtracker.gui;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class PathOverlay extends Overlay {

	// PRIVATE FIELDS
	
	// Debug
	//private static final String TAG = "TourTracker";
	//private static final boolean D = true;
	
	private List<GeoPoint> geoPoints;
	private Paint paint;
		
	// CTOR
	
	public PathOverlay() {
		
		geoPoints = new ArrayList<GeoPoint>();
		
		paint = new Paint();
		paint.setDither(true);
		paint.setColor(Color.BLUE);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStrokeWidth(6);
		paint.setAlpha(50);
		
	}
	
	// OVERRIDES
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow){
        super.draw(canvas, mapView, shadow);
		
		// Check for at least 2 points
		if (geoPoints.size() < 2) {
			return;
		}
		
		Projection projection = mapView.getProjection();
		Point point = new Point();		
		Path path = new Path();
		
		// Starting point
		GeoPoint startGeoPoint = geoPoints.get(0);
        projection.toPixels(startGeoPoint, point);
        path.moveTo(point.x, point.y);
        
        // All the next points
        for (GeoPoint geoPoint : geoPoints)
        {
        	point = new Point();
            projection.toPixels(geoPoint, point);            
            path.lineTo(point.x, point.y);
            path.moveTo(point.x, point.y);            
        }

        canvas.drawPath(path, paint);        
    }	
	
	public void addPoint(double latitude, double longitude) {
		geoPoints.add(new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6)));
	}
	
	public void addPoints(List<dk.itu.spvc.tourtracker.model.Point> points) {
		for (dk.itu.spvc.tourtracker.model.Point point : points){
			geoPoints.add(new GeoPoint((int) (point.getLatitude() * 1E6), (int) (point.getLongitude() * 1E6)));
		}		
	}
	
	public void clear() {
		geoPoints.clear();
	}
}
